//
//  TODOToolManager.swift
//  TODOApp
//
//  Created by chenyh on 2018/5/24.
//  Copyright © 2018年 chenyh. All rights reserved.
//

struct TODOToolManager {
    
    struct CommonTool {
        
        static var getCurrDisplayOnWindowVC: UIViewController?{
            
            get{
                
                let rootWindow =  AppDelegate.shared.window!
                
                if let rootVC = rootWindow.rootViewController as? UITabBarController {
                    
                    if let rootVC_SelectedVC = rootVC.selectedViewController as? UINavigationController
                    {
                        return rootVC_SelectedVC.viewControllers.last ?? nil
                        
                    }else{
                        return rootVC.selectedViewController ?? nil
                    }
                    
                    
                }else if let rootVC = rootWindow.rootViewController as? UINavigationController
                {
                    return rootVC.viewControllers.last ?? nil
                    
                }else{return nil}
                
            }
            
        }
        
        @discardableResult
        static func DQ_BaseUrl() -> String {
            if let deviceToken = UserDefaults.standard.object(forKey: "TODO_BaseUrl") as? String {
                return deviceToken
            }
            
            return "http://0.0.0.0:8888"
        }
        
        static func changeURLBasePath(type: ServerEnv) {
            
            switch type {
            case .dev:
                setTodo_BaseUrl(token: "http://0.0.0.0:8888")
            case .test:
                setTodo_BaseUrl(token: "xxx")
            case .pro:
                setTodo_BaseUrl(token: "xxx")
            }
            
        }
        
        @discardableResult
        static func getHostPath() -> String {
            //api/v1
            return DQ_BaseUrl() + "/api" + "/v1"
        }
        
        static func setTodo_BaseUrl(token: String) {
            UserDefaults.standard.set(token, forKey: "TODO_BaseUrl")
            UserDefaults.standard.synchronize()
            
        }
        
        @discardableResult
        static func getMMSSFromSS(s: Int) -> String {
            
            
            var s_minute = "\(s/60)"
            if s / 60 < 10 {
                s_minute = "0\(s/60)"
            }
            
            let s_second = "\(s%60)"
            
            
            let f_time = String(format: "%@:%@",s_minute,s_second)
            
            return f_time
            
            
        }
        @discardableResult
        static func getHHMMSSFromSS(s: Int) -> String {
            
            
            
            var h_hour = "\(s/3600)"
            
            if s/3600 < 10 {
                h_hour = "0\(s/3600)"
            }
            
            var s_minute = "\((s%3600)/60)"
            
            if (s%3600)/60 < 10 {
                s_minute = "0\((s%3600)/60)"
            }
            
            var s_second = "\(s%60)"
            if s%60 < 10 {
                s_second = "0\(s%60)"
            }
            
            let f_time = String(format: "%@:%@:%@",h_hour,s_minute,s_second)
            
            return f_time
            
            
        }
        
        @discardableResult
        static func isLogin() -> Bool {
            if let account = Account.unarchiveObject() {
                return !account.token.isEmpty
            }
            return false
            
        }
        
        @discardableResult
        static func getToken() -> String {
            if let account = Account.unarchiveObject() {
                print(account.token)
                return account.token
            }
            return ""
        }
        
        @discardableResult
        static func getUid() -> String {
            if let account = Account.unarchiveObject() {
                return account.uid
            }
            return ""
        }
    }
    
    
}
